package com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.adhoc;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.BitAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.internalDownload;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Site_Info;

/**
 * @author lt
 * @version 1.0
 * @date 2021-04-18 19:43:58
 * @email okprog@sina.com
 */
public class BitAutoForumReplySimpleCrawlerSchedulerJob extends BitAutoCommonCrawlerSchedulerJob {

    private static final String crawler_level = "simple";
    private static final String meta_site="sgmKw";//adhocKw bmwKw sgmKw

    public static void main(String[] args) {
        publishBitAutoCrawlerSchedulerJobInfo();
    }

    public static CrawlerJob publishBitAutoCrawlerSchedulerJobInfo(){
        CrawlerJob bitAutoScheduler = bitAutoScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(bitAutoScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("易车论坛最新回复回溯采集任务");
        crawlerScheduleJob.setCrawlerKey(bitAutoScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return bitAutoScheduler;
    }

    private static final String siteInfo = "sgm-20210818-20211104";
    public static CrawlerJob bitAutoScheduler(){
        //关键词 source record
        CrawlerRequestRecord keywordCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("bitauto_keywords",turnPageItem)
                .httpUrl(metaServiceHttpPrefix + "/v1/meta/"+domain+"/keys?site="+meta_site)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        CrawlerRequestRecord indexRecord = CrawlerRequestRecord.builder()
                .startPageRequest("bitauto_index",turnPageItem)
                .httpUrl("https://www.bitauto.com/")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        //论坛-最新回采集
        CrawlerRequestRecord forumReplyCrawler = doForumReplyCrawler();
        String site = forumReplyCrawler.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");

        return CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,siteInfo,crawler_level,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain, site, crawler_level,"queue")))
                .kafkaResultPipeline(null,kafkaTopciForTraceJob,null)
                .fileResultPipeline(null,"/data/chance_crawler_runner/logs/node/bitauto/forum_simple.log",false)
                .crawlerJobThreadNumber(20)
                .requestRecord(forumReplyCrawler)
                .supportRecord(keywordCrawlerRecord)
                .supportRecord(indexRecord)
                .build();
    }


    public static CrawlerRequestRecord doForumReplyCrawler() {
        String site = "forum_reply";

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .recordKey("https://www.bitauto.com/")
                .httpUrl("https://www.bitauto.com/")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .domain(domain)
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(domain))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(0,  new long[]{1629216000000L,System.currentTimeMillis()}))
                .proxy(proxy)
                .needParsed(false)
                .needWashed(false)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);

        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(site);
        requestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,siteInfo);//forum-bmw-20210924 forum-simple-20210924
        return requestRecord;
    }
}
